var host = document.domain; //'222.242.155.42'; //
var serverUrl = 'http://'+host+':12306/monk';
var imageUrl = 'http://'+host+':12309';
function ajax(opts){
  $.ajax({
    url: opts.url.indexOf("http://") == -1 ? (serverUrl + opts.url) : opts.url,
    contentType : opts.contentType || 'application/x-www-form-urlencoded',
    data : null != opts.contentType && opts.contentType == 'application/json' ? JSON.stringify(opts.data) : opts.data,
    type: opts.type || 'post',
    dataType: opts.dataType || 'json',
    beforeSend: function(xhr){
      showLoading();
      if(opts.beforeSend)
        opts.beforeSend(xhr);
    },
    complete: function(xhr, ts){
      hideLoading();
      if(opts.complete)
        opts.complete(xhr, ts);
    },
    success: function(res){
      if(res.code == 0){
        opts.success(res.data);
      }else{
        alert(res.msg);
      }
    },
    error: function(a,b,c){
      alert(b)
    }
  });
}
function showLoading(){
  $("#loadingWin").show();
}
function hideLoading(){
  $("#loadingWin").hide();
}
//对Date的扩展，将 Date 转化为指定格式的String
//月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
//年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
//例子：
//(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
//(new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
Date.prototype.format = function (fmt) {
var o = {
  "M+": this.getMonth() + 1, // 月份
  "d+": this.getDate(), // 日
  "h+": this.getHours(), // 小时
  "m+": this.getMinutes(), // 分
  "s+": this.getSeconds(), // 秒
  "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
  "S": this.getMilliseconds() // 毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
window.cookieStorage = (new (function(){
  var maxage = 60*60*1000 * 24 * 365;
  var path = '/';
  var cookie = getCookie();

  function getCookie(){
    var cookie = {};
    var all = document.cookie;
    if(all === "")
      return cookie;
    var list = all.split("; ");
    for(var i=0; i < list.length; i++){
      var cookies = list[i];
      var p = cookies.indexOf("=");
      var name = cookies.substring(0,p);
      var value = cookies.substring(p+1);
      value = decodeURIComponent(value);
      cookie[name] = value;
    }
    return cookie;
  }

  var keys = [];
  for(var key in cookie)
    keys.push(key);
  this.length = keys.length;
  this.key = function(n){
    if(n<0 || n >= keys.length)
      return null;
    return keys[n];
  };

  this.setItem = function(key, value){
    if(! (key in cookie)){
      keys.push(key);
      this.length++;
    }
    cookie[key] = value;
    var cookies = key + "=" +encodeURIComponent(value);
    if(maxage)
      cookies += "; max-age=" + maxage;
    if(path)
      cookies += "; path=" + path;
    cookies += "; domain=hnkbmd.com";
    document.cookie = cookies;
  };

  this.getItem = function(name){
    return cookie[name] || null;
  };

  this.removeItem = function(key){
    if(!(key in cookie))
      return;
    delete cookie[key];
    for(var i=0; i<keys.length; i++){
      if(keys[i] === key){
        keys.splice(i, 1);
        break;
      }
    }
    this.length--;
    document.cookie = key + "=; max-age=0; domain=hnkbmd.com";
  };

  this.clear = function(){
    for(var i=0; i<keys.length; i++)
      document.cookie = keys[i] + "; max-age=0; domain=hnkbmd.com";
    cookie = {};
    keys = [];
    this.length = 0;
  };
})());

window.starStorage = (new (function(){
  var storage;
  if(window.sessionStorage){
    storage = sessionStorage;
  }else {
    storage = cookieStorage;
  }
  this.setItem = function(key, value){
    storage.setItem(key, value);
  };
  this.getItem = function(name){
    return storage.getItem(name);
  };
  this.removeItem = function(key){
    storage.removeItem(key);
  };
  this.clear = function(){
    storage.clear();
  };
})());

var loginKey = "login_key_chenzhou";
function setLoginInfo(user){
  starStorage.setItem(loginKey, JSON.stringify(user));
}
function logout(){
  starStorage.removeItem(loginKey);
}
function getLoginInfo(){
  var tem = starStorage.getItem(loginKey);
  if(!tem) return null;
  var user = JSON.parse(tem);
  return user.userId > 0 ? user : null;
}
function isLogin(){
  return true;
  try{
    var user = getLoginInfo();
    return null != user;
  }catch(e){
    starStorage.removeItem(loginKey);
    return false;
  }
}
